﻿package DevilCat{
	import flash.events.Event;
	import flash.net.*;
	import flash.display.MovieClip;	
	import DevilCat.*;
	public class SQLData {
		public static var uid:Number=0;
		public static var username:String="";
		public static var password:String="";
		public static var usecurity:Number=0;
		public static var miles:Number=0;
		public static var pmp:Number=0;
		public static var uflag:String="1";
		
		public static var movesXML:XML;
		public static var pokemonsXML:XML;
		public static var mapsXML:XML;
		public static var itemsXML:XML;
		public static var pokemonList:Array;
		public static var pokemonAllList:Array;
		public static var itemList:Array;
		
		public static var mainClip:MovieClip;
		public static var battleClip:MovieClip;
		public static function NewPokemonObjectFromXMLList(PokemonData:XMLList):PokemonObject{
			var Pokemon:PokemonObject;
			if(String(PokemonData.Move1)==""){
				Pokemon= new PokemonObject(pokemonsXML[PokemonData.Name],movesXML[pokemonsXML[PokemonData.Name].Move1],movesXML[pokemonsXML[PokemonData.Name].Move2],movesXML[pokemonsXML[PokemonData.Name].Move3],movesXML[pokemonsXML[PokemonData.Name].Move4]);
			}else{
				Pokemon= new PokemonObject(pokemonsXML[PokemonData.Name],movesXML[PokemonData.Move1],movesXML[PokemonData.Move2],movesXML[PokemonData.Move3],movesXML[PokemonData.Move4]);
			}
			if (String(PokemonData.Rate)!="") {
				Pokemon.Rate=Number(PokemonData.Rate);
			}
			if (String(PokemonData.Evasive)!="") {
				Pokemon.Evasive=Number(PokemonData.Evasive);
			}
			if (String(PokemonData.Accuracy)!="") {
				Pokemon.Accuracy=Number(PokemonData.Accuracy);
			}
			//Pokemon.recoveryStatByMax();
			if(String(PokemonData.Ability)!="") {
				Pokemon.Ability=PokemonData.Ability;
			}
			Pokemon.addBonus(Number(PokemonData.HP),Number(PokemonData.Attack),Number(PokemonData.Defense),Number(PokemonData.SpAtk),Number(PokemonData.SpDef),Number(PokemonData.Speed));
			
			return Pokemon;
		}
		public static function StringID(_id:uint):String{
			if(_id<10) return "00"+_id;
			if(_id<100) return "0"+_id;
			return	""+_id;
		}
		public static function ListItemAddEventUseItem(){			
			for (var i:int =0; i<SQLData.itemList.length; i++)
			{
				SQLData.itemList[i].AddEventUseItem();
				SQLData.itemList[i].reText();
			}
		}
		public static function AddPokemonFromXML(PokemonSQLList:XMLList,reloadMode:String,leadpkm:Number){
			var i:int;
			var pkmlist:Number=PokemonSQLList.pkm.length();
			switch (reloadMode){
				case "new":
					pokemonList=new Array();
					pokemonAllList=new Array();
					for(i =0;i<pkmlist;i++){;
						pokemonAllList.push(NewPokemonObjectFromXMLList(new XMLList(PokemonSQLList.pkm[i])));
						pokemonAllList[i].upid=PokemonSQLList.pkm[i].upid;
						pokemonAllList[i].pmod=Number(PokemonSQLList.pkm[i].pmod);
						pokemonAllList[i].setStat(PokemonSQLList.pkm[i].HP,PokemonSQLList.pkm[i].Attack,PokemonSQLList.pkm[i].Defense,PokemonSQLList.pkm[i].SpAtk,PokemonSQLList.pkm[i].SpDef,PokemonSQLList.pkm[i].Speed);
						//pokemonAllList[i].setStat(PokemonSQLList.pkm[i].HP,PokemonSQLList.pkm[i].Attack,PokemonSQLList.pkm[i].Defense,PokemonSQLList.pkm[i].SpAtk,PokemonSQLList.pkm[i].SpDef,PokemonSQLList.pkm[i].Speed,PokemonSQLList.pkm[i].Total);
						pokemonAllList[i].CurrentHP=PokemonSQLList.pkm[i].CurrentHP;
						pokemonAllList[i].Status=PokemonSQLList.pkm[i].Status;
						if(pokemonAllList[i].pmod==0){
							pokemonList.push(pokemonAllList[i]);
						}
					}		
					break;
				case "insert":
					pokemonList[pkmlist-1].upid=PokemonSQLList.pkm[pkmlist-1].upid;
					break;
			}
			if(leadpkm>0){		
				var ipos:int=pokemonList.length;
				for(i =0;i<ipos;i++){			
					if(pokemonList[i].upid==leadpkm){
						var tempPkm:PokemonObject=pokemonList[i];
						pokemonList[i]=pokemonList[0];
						pokemonList[0]=tempPkm;
						break;
					}			
				}	
			}
		}
		public static function PVPAddPokemonFromXML(PokemonSQLList:XMLList,leadpkm:Number){
			var i:int;
			var pkmlist:Number=PokemonSQLList.pkm.length();
			var newPkmList:Array=new Array();
			for(i =0;i<pkmlist;i++){;
				newPkmList.push(NewPokemonObjectFromXMLList(new XMLList(PokemonSQLList.pkm[i])));
				newPkmList[i].upid=PokemonSQLList.pkm[i].upid;
				newPkmList[i].pmod=Number(PokemonSQLList.pkm[i].pmod);
				newPkmList[i].setStat(PokemonSQLList.pkm[i].HP,PokemonSQLList.pkm[i].Attack,PokemonSQLList.pkm[i].Defense,PokemonSQLList.pkm[i].SpAtk,PokemonSQLList.pkm[i].SpDef,PokemonSQLList.pkm[i].Speed);
				newPkmList[i].CurrentHP=PokemonSQLList.pkm[i].CurrentHP;
				newPkmList[i].Status=PokemonSQLList.pkm[i].Status;
			}		
			if(leadpkm>0){		
				var ipos:int=newPkmList.length;
				for(i =0;i<ipos;i++){			
					if(newPkmList[i].upid==leadpkm){
						var tempPkm:PokemonObject=newPkmList[i];
						newPkmList[i]=newPkmList[0];
						newPkmList[0]=tempPkm;
						break;
					}			
				}	
			}
			return newPkmList;
		}
		public static function AddItemFromXML(itemSQLList:XMLList){
			itemList=null;
			itemList=new Array();
			var itemLeng:Number=itemSQLList.it.length();
			for(var i:int =0;i<itemLeng;i++){	
				//$var itemNode:XMLList=itemsXML[String(itemSQLList.it[i].Code)];
				//itemsXML[itemSQLList.it[i].Name]
				//public function ItemObject(iiid:Number,iCode:String,iName:String,iStat:String,iEffect:Number,iCost:Number,iNumberOf:Number,iType:Number=0) {
				//$AddItem(new ItemObject(Number(itemSQLList.it[i].iid),String(itemSQLList.it[i].Code),String(itemNode.Name),String(itemNode.Stat),Number(itemNode.Effect),Number(itemNode.Cost),Number(itemSQLList.it[i].NumberOf)),false);
				//AddItem(new ItemObject(String(itemSQLList.it[i].Name),Number(itemSQLList.it[i].Effect),Number(itemSQLList.it[i].NumberOf)),false);
				AddItem(GetItemFromXML(itemSQLList.it[i]),false);
			}
		}
		public static function GetItemFromXML(itemReal:XML):ItemObject{
			var itemBase:XMLList=itemsXML[String(itemReal.Code)];
			return new ItemObject(Number(itemReal.iid),String(itemReal.Code),String(itemBase.Name),String(itemBase.Stat),Number(itemBase.Effect),Number(itemBase.Cost),Number(itemReal.NumberOf),String(itemBase.Type))
		}
		public static function AddItem(_item:ItemObject,addNew:Boolean =true) {
			var iPos:int =CheckItemPos(_item);
			if(iPos<0){
				itemList.push(_item);
				if(addNew)
					InserItemSQL(_item);
			}else{
				itemList[iPos].NumberOf+=_item.NumberOf;
				if(addNew){
					itemList[iPos].reText();
					UpdateItemSQL(itemList[iPos],_item.NumberOf);
				}
			}
		}
		public static function DeleteItemZero() {
			for (var i:int =0; i<itemList.length; i++) {
				if (itemList[i].NumberOf<1) {
					itemList.splice(i,1);
				}
			}
		}

		public static function LoadItemPanel(_Panel:MovieClip) {
			while (_Panel.numChildren > 0) {
				_Panel.removeChildAt(0);
			}
			var countNumberOf:int=0;
			for (var i:int =0; i<SQLData.itemList.length; i++) {
				if(SQLData.itemList[i].NumberOf>0){
					SQLData.itemList[i].y=235-24*countNumberOf;
					SQLData.itemList[i].reText();
					SQLData.itemList[i].AddEventUseItem();
					_Panel.addChild(SQLData.itemList[i]);
					countNumberOf++
				}
			}
		}
		public static function CheckItemPos(_item:ItemObject):int{
			var i:Number;
			for (i=0; i<itemList.length; i++) {
				if (itemList[i].Name==_item.Name) {
					return i;
				}
			}
			return -1;
		}
		public static function SendQuery(Query:String, submit:String="DoQuery") {
			var myData:URLRequest = new URLRequest("twalk_function.php");
			myData.method = URLRequestMethod.POST;
			var variables:URLVariables = new URLVariables();
			variables.uid = uid;
			variables.usecurity = usecurity;
			variables.query = Query;
			variables.submit = submit;
			myData.data = variables;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = new XML;
			loader.addEventListener(Event.COMPLETE, dataQuery);
			loader.load(myData);
		}
		public static function dataQuery(evt:Event):void {
			var XMLData = new XML(evt.target.data);
			uflag=XMLData.uflag;
		}
		public static function changePMP(amount:Number):Boolean{
			if(pmp+amount>0){
				pmp+=amount;
				mainClip.setNewInforPanel();
				var Query:String="UPDATE twalk_user SET upmp=upmp+"+amount+" WHERE uid='"+uid+"'";
				SendQuery(Query);
				return true;
			}			
			//mainClip.addNewFloatText(amount+" PMP",250,220);			
			return false;
		}
		public static function changeUserEvent(eventLocation:Number,eventNumber:Number){
			var eNumber=Number(eventNumber);
			if(eNumber>0){
				eNumber++;
				var Query:String="UPDATE twalk_userevent SET "+getEventName(eventLocation)+"='"+eNumber+"' WHERE uid='"+uid+"'";
				SendQuery(Query);
			}
		}
		public static function getEventName(eventLocation:Number):String{
			switch (eventLocation){
				case 1: return "MainEvent";
				case 3: return "Celebi";
				default: return "FakeEvent";
			}
		}
		public static function setID(ruid:Number,rusecurity:Number,rpmp:Number) {
			uid=ruid;
			usecurity=rusecurity;
			pmp=rpmp;
		}
		public static function GetData(submit:String, completeFunction:Function,query:String=" ") {
			var myData:URLRequest = new URLRequest("twalk_function.php");
			myData.method = URLRequestMethod.POST;
			var variables:URLVariables = new URLVariables();
			variables.uid = uid;
			variables.username = username;
			variables.password = this.password;
			variables.usecurity = usecurity;
			variables.query = query;
			variables.submit = submit;
			//mainClip.Trace(variables.uid+" "+variables.username+" "+usecurity);
			myData.data = variables;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = new XML;
			//loader.dataFormat = new URLLoaderDataFormat.TEXT
			loader.load(myData);			
			loader.addEventListener(Event.COMPLETE, completeFunction);
		}

		public static function UpdateItemSQL(item:ItemObject,_value:Number) {
			var Query:String="UPDATE twalk_useritem SET NumberOf=NumberOf+"+_value+"  WHERE uid='"+uid+"' AND iid='"+item.iid+"'";
			//var Query:String="UPDATE twalk_useritem SET NumberOf=NumberOf+"+_value+"  WHERE uid='"+uid+"' AND iid='"+item.Name+"' AND Effect='"+item.Effect+"'";
			SendQuery(Query);
		}
		public static function InserItemSQL(item:ItemObject) {
			var Query:String="INSERT INTO twalk_useritem (uid,Code,NumberOf) VALUE ('"+uid+"','"+item.Code+"','"+item.NumberOf+"')";;
			//var Query:String="INSERT INTO twalk_useritem (uid,Name,Effect,NumberOf,Type) VALUE ('"+uid+"','"+item.Name+"','"+item.Effect+"','"+item.NumberOf+"','"+item.Type+"')";;
			SendQuery(Query);
		}
		public static function UpdatePokemonSQL(pokemon:PokemonObject) {
			var Query:String="UPDATE twalk_pokemon SET pid='"+pokemon.pid+"' ,Name='"+pokemon.Name+"' ,Ability='"+pokemon.Ability+"' ,Status='"+pokemon.Status+"' ,CurrentHP='"+pokemon.CurrentHP+"' ,HP='"+pokemon.HP+"' ,Attack='"+pokemon.Attack+"' ,Defense='"+pokemon.Defense+"' ,SpAtk='"+pokemon.SpAtk+"' ,SpDef='"+pokemon.SpDef+"' ,Speed='"+pokemon.Speed+"' ,Total='"+pokemon.Total+"' ,Move1='"+pokemon.Move1.Name+"' ,Move2='"+pokemon.Move2.Name+"' ,Move3='"+pokemon.Move3.Name+"' ,Move4='"+pokemon.Move4.Name+"' WHERE uid='"+uid+"' AND upid='"+pokemon.upid+"'";
			SendQuery(Query);
		}
		public static function InsertPokemonSQL(pokemon:PokemonObject) {
			var Query:String="INSERT INTO twalk_pokemon (pid,uid,Name,Ability,HP,CurrentHP,Attack,Defense,SpAtk,SpDef,Speed,Move1,Move2,Move3,Move4) VALUE ( '"+pokemon.pid+"','"+uid+"','"+pokemon.Name+"','"+pokemon.Ability+"','"+pokemon.HP+"','"+pokemon.CurrentHP+"','"+pokemon.Attack+"','"+pokemon.Defense+"','"+pokemon.SpAtk+"','"+pokemon.SpDef+"','"+pokemon.Speed+"','"+pokemon.Move1.Name+"','"+pokemon.Move2.Name+"','"+pokemon.Move3.Name+"','"+pokemon.Move4.Name+"')";
			SendQuery(Query);
		}
		public static function ChangeMap(Name:String,Miles:Number) {
			var Query:String="UPDATE twalk_user SET Map='"+Name+"',Mile='"+Miles+"'  WHERE uid='"+uid+"'";
			SendQuery(Query);
		}

	}
}